package com.xiaojie.learn.spring.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;

@Component
public class TitleService {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private TransactionTemplate transactionTemplate;

    //模拟业务操作1
    public void bus1(){
        this.transactionTemplate.executeWithoutResult(transactionStatus -> {
            //先删除表数据
            this.jdbcTemplate.update("delete from t_title");
            //调用bus2
            this.bus2();
        });
    }

    //模拟业务操作2
    public void bus2(){
        this.transactionTemplate.executeWithoutResult(transactionStatus -> {
            this.jdbcTemplate.update("insert into t_title (title) values (?)","java");
            this.jdbcTemplate.update("insert into t_title (title) values (?)","spring");
            this.jdbcTemplate.update("insert into t_title (title) values (?)","mybatis");
        });
    }

    //查询表中所有数据
    public List list(){
        return this.jdbcTemplate.queryForList("select * from t_title");
    }

}
